#ifndef __MYIIC_H
#define __MYIIC_H

#include "main.h"
#include "gd32f20x_eval.h"
#include "gd32f20x.h"
/******************************************************************************************/

//PC9 SDA PA8 SCL
#if 1
#define IIC_SCL_GPIO_PORT               GPIOA
#define IIC_SCL_GPIO_PIN                GPIO_PIN_8

#define IIC_SDA_GPIO_PORT               GPIOC
#define IIC_SDA_GPIO_PIN                GPIO_PIN_9
#endif


/******************************************************************************************/
typedef struct {
    uint32_t    sclport;
    uint32_t    sdaport;
    uint32_t    sclpin;
    uint32_t    sdapin;
}_myiic_t;

#define IIC_SCL(myiic_ptr,x)       do{ x ? \
                              gpio_bit_write(myiic_ptr->sclport, myiic_ptr->sclpin, SET) : \
                              gpio_bit_write(myiic_ptr->sclport, myiic_ptr->sclpin, RESET); \
                          }while(0)       /*  SCL */

#define IIC_SDA(myiic_ptr,x)      do{ x ? \
                              gpio_bit_write(myiic_ptr->sdaport, myiic_ptr->sdapin, SET) : \
                              gpio_bit_write(myiic_ptr->sdaport, myiic_ptr->sdapin, RESET); \
                          }while(0)       /* SDA */

#define IIC_READ_SDA(myiic_ptr)     gpio_input_bit_get(myiic_ptr->sdaport, myiic_ptr->sdapin)


void iic_start(_myiic_t *i2c);

void iic_stop(_myiic_t *i2c);

uint8_t iic_wait_ack(_myiic_t *i2c);

void iic_ack(_myiic_t *i2c);

void iic_nack(_myiic_t *i2c);

void iic_send_byte(_myiic_t *i2c,uint8_t data);

uint8_t iic_read_byte(_myiic_t *i2c, uint8_t ack);

#endif

